<template>
  <div>
    <Classification :isDisableShow="!isAttrShow"></Classification>
    <el-card v-if="isAttrShow">
      <!-- 列表展示 -->
      <el-button
        type="primary"
        icon="el-icon-plus"
        @click="addAttrList"
        :disabled="attrList.length == 0"
      >
        添加属性
      </el-button>
      <el-table class="mt_20" :data="attrList" border>
        <el-table-column label="序号" type="index" width="160" align="center">
        </el-table-column>
        <el-table-column label="属性名" align="center" prop="attrName">
        </el-table-column>
        <el-table-column label="属性值列表" align="center">
          <template slot-scope="scope">
            <el-tag
              type="success"
              class="mr_10"
              v-for="item in scope.row.attrValueList"
              :key="item.id"
              >{{ item.valueName }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{ row, $index }">
            <el-button
              type="warning"
              icon="el-icon-edit"
              size="small"
              @click="editAttr(row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="small"
              @click="deleteAttrList(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-card v-else>
      <!-- 添加数据页面 -->
      <el-form class="mt_20">
        <el-form-item label="属性名" inline>
          <el-input
            minlength="160"
            style="width: 200px"
            v-model="attrForm.attrName"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-button
        type="primary"
        class="mr_20"
        @click="addAttrValue"
        :disabled="attrForm.attrName == ''"
        >添加属性值</el-button
      >
      <el-button @click="changIsShow">取消</el-button>
      <el-table class="mt_20 mb_20" border :data="attrForm.attrValueList">
        <el-table-column
          label="序号"
          type="index"
          align="center"
          width="160"
        ></el-table-column>
        <el-table-column label="属性值名称" key="slot">
          <template v-slot="{ row, $index }">
            <el-input
              ref="inputRef"
              v-model="row.valueName"
              @blur="inputBlur(row, $index)"
              v-if="row.inputVisible"
            ></el-input>
            <div v-else @click="clickHandler(row)">{{ row.valueName }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row, $index }">
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="deleteAttrValue($index)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button type="primary" class="mr_20" @click="onSave">保存</el-button>
      <el-button @click="changIsShow">取消</el-button>
    </el-card>
  </div>
</template>

<script>
import infoListApi from "@/api/pro/infoCatd";
import { cloneDeep } from "loadsh";
import { mapState } from "vuex";
export default {
  name: "Attr",
  data() {
    return {
      isAttrShow: true,
      attrList: [],
      attrForm: {
        attrName: "",
        categoryId: undefined,
        categoryLevel: 3,
        attrValueList: [],
      },
      isDisableShow: false,
    };
  },
  computed: {
    ...mapState("category", ["category1Id", "category2Id", "category3Id"]),
  },
  methods: {
    // 初始化添加表单数据
    infoAttrForm() {
      return {
        attrName: "",
        categoryId: undefined,
        categoryLevel: 3,
        attrValueList: [],
      };
    },
    // 获取平台属性列表的功能函数
    async getAttrList() {
      let { category1Id, category2Id, category3Id } = this;
      try {
        let result = await infoListApi.getAttrInfoList(
          category1Id,
          category2Id,
          category3Id
        );
        this.attrList = result.data;
      } catch (error) {
        console.log(error);
      }
    },
    // 点击添加属性按钮的回调
    addAttrList() {
      this.isAttrShow = !this.isAttrShow;
    },
    // 点击取消按钮的回调
    changIsShow() {
      this.isAttrShow = !this.isAttrShow;
      this.attrForm = this.infoAttrForm();
      this.getAttrList();
    },
    // 点击添加属性值的回调
    addAttrValue() {
      this.attrForm.attrValueList.push({
        valueName: ``,
        inputVisible: true,
      });
      this.$nextTick(() => {
        this.$refs.inputRef.focus();
      });
    },
    // 失去焦点的回调
    inputBlur(row, index) {
      // 需要判断input输入的有没有内容,如果没有内容,该数据不应该存在
      if (!row.valueName) {
        this.attrForm.attrValueList.splice(index, 1);
        alert("输入内容不能为空");
        return;
      }
      // 去重
      // attrForm.value.attrValueList  中 查找 有没有和当前 row.valueName 相同的
      // isRepeat 是否有重复(需要把自己排除掉)
      const isRepeat = this.attrForm.attrValueList.some((item, idx) => {
        if (index === idx) {
          // 代表是自己,把自己排除掉
          return false;
        }
        return item.valueName === row.valueName;
      });
      if (isRepeat) {
        // 代表有重复
        this.attrForm.attrValueList.splice(index, 1);
        alert("输入内容重复,请重新输入");
        return;
      }
      row.inputVisible = false;
    },
    // 点击文本内容的回调
    clickHandler(row) {
      row.inputVisible = true;
      this.$nextTick(() => {
        this.$refs.inputRef.focus();
      });
    },
    // 点击保存的回调
    async onSave() {
      this.attrForm.categoryId = this.category3Id;
      try {
        let result = await infoListApi.saveAttrInfo(this.attrForm);
        this.getAttrList();
        this.changIsShow();
      } catch (error) {
        console.log(error);
        console.log("保存失败，请重试");
      }
    },
    // 点击删除按钮的回调
    deleteAttrValue(index) {
      this.attrForm.attrValueList.splice(index, 1);
    },
    //点击编辑按钮的回调
    editAttr(row) {
      this.attrForm = cloneDeep(row);
      this.attrForm.attrValueList = cloneDeep(row).attrValueList.map((e) => {
        e.inputVisible = false;
        return e;
      });
      this.isAttrShow = false;
    },
    // 点击删除属性的回调
    deleteAttrList(row) {
      this.$confirm(`确认要删除${row.attrName}吗？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            let result = await infoListApi.deleteAttr(row.id);
            this.getAttrList();
            this.$message({
            type: "success",
            message: "删除成功!",
          });
          } catch (error) {
            console.log(error);
          }
          
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  mounted() {
    this.$bus.$on("getList", this.getAttrList);
  },
};
</script>

<style>
</style>